﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using ToolUtil;
using zyconfig;

namespace zyui
{
    public partial class DigitalizeSearch : UserControl
    {
        public int smklx = 0, _kjm;
        private List<string> DbKayList;
        private List<string> DbTitleList;
        private List<string> SearchZdList;
        private List<string> FiledsList;
        public event btnclick BtnScarchClick;
        public string Sql = string.Empty, _DataType;
        public string ystime = string.Empty, yetime = string.Empty;
        public bool fsFull=false;
        public DigitalizeSearch()
        {
            InitializeComponent();
        }
        public void AddConfig(int smk,object mrq)
        {
            this.smklx = smk;
            if (mrq ==null)
            {
                DateTime year = DateTime.Now.AddDays(-180);
                DateChoser.AddTo(this.sTime, year);
                DateTime year1 = DateTime.Now.AddDays(-15);
            }
            else
            {
                DateTime dt = Convert.ToDateTime(mrq);
                DateChoser.AddTo(this.sTime, dt);
            }
            DateTime aday = DateTime.Now.AddDays(1);
            DateChoser.AddTo(this.eTime, aday);
            this.pzdKay.SelectedIndex = 0;
            this.pfsKay.SelectedIndex = 0;
            this.AddDbList();
        }
        /// <summary>
        /// 获取书目库
        /// </summary>
        public void AddDbList()
        {
            this.skKay.Items.Clear();
            this.DbKayList = new List<string>();
            this.DbKayList.Clear();
            using (DbOperHandler _open = new Common().Doh())
            {
                string sql = "select * from 书目数据库定义 where  表文件名='情报检索库' order by 库键码";
                _open.Reset();
                _open.SqlCmd = sql;
                DataTable dt = _open.GetDataTable();
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dRow in dt.Rows)
                    {
                        this.skKay.Items.Add(dRow["书目库名"].ToString().Trim());
                        this.DbKayList.Add(dRow["库键码"].ToString());
                    }
                }
                if (this.skKay.Items.Count > 0)
                {
                    this.skKay.SelectedIndex = 0;
                    this._kjm = int.Parse(this.DbKayList[0].ToString());
                }
            }
        }
        private void skKay_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedIndex = this.skKay.SelectedIndex;
            this._kjm = int.Parse(this.DbKayList[selectedIndex].ToString());
            this.jsxKay.Items.Clear();
            this.DbTitleList = new List<string>();
            this.SearchZdList = new List<string>();
            this.FiledsList = new List<string>();
            this.FiledsList.Clear();
            this.SearchZdList.Clear();
            this.DbTitleList.Clear();
            using (DbOperHandler _open = new Common().Doh())
            {
                _open.Reset();
                string sql = "select * from 检索途径定义 where 库键码=" + this._kjm;
                if (smklx == 0)
                    _open.SqlCmd = sql + " and 馆藏维护浏览顺序 is not null order by 馆藏维护浏览顺序";
                if (smklx == 1)
                    _open.SqlCmd = sql + " and 采购浏览顺序 is not null order by 采购浏览顺序";
                if (smklx == 2)
                    _open.SqlCmd = sql + " and 编目浏览顺序 is not null order by 编目浏览顺序";
                if (smklx == 3)
                    _open.SqlCmd = sql + " and 典藏分配浏览顺序 is not null order by 典藏分配浏览顺序";
                //Console.WriteLine(_open.SqlCmd);
                DataTable dt = _open.GetDataTable();
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dRow in dt.Rows)
                    {
                        string tname = dRow["所属表"].ToString().Trim();
                        string zdm = dRow["字段名称"].ToString().Trim();
                        this.jsxKay.Items.Add(zdm);
                        this.DbTitleList.Add(dRow["所属表"].ToString());
                        this.SearchZdList.Add(zdm);
                        
                        if (tname.Equals("情报检索库"))
                        {
                            try
                            {
                                bool xs = Convert.ToBoolean(dRow["是否显示"].ToString().Trim());
                                if (xs)
                                {
                                    this.FiledsList.Add(zdm);
                                }
                            }
                            catch { }
                        }
                    }
                }
                if (this.jsxKay.Items.Count > 0)
                {
                    this.jsxKay.SelectedIndex = 0;
                }
            }
        }
        private void jsxKay_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = this.jsxKay.SelectedIndex;
            string tableName = this.DbTitleList[index].ToString().Trim();
            string fildName = this.SearchZdList[index].ToString().Trim();
            this.jsfsKay.Items.Clear();
            using (DbOperHandler _open = new Common().Doh())
            {
                string sql = "所属表='" + tableName + "' and 字段名称='" + fildName + "'";
                _open.Reset();
                _open.ConditionExpress = sql;
                string zdlx = _open.GetField("检索途径定义", "字段类型", false).ToString();
                if (zdlx.ToLower().Equals("c"))
                {
                    if (fildName.Trim().Equals("登录号") || fildName.Trim().Equals("条形码"))
                        this.jsfsKay.Items.Add("介于");
                    this.jsfsKay.Items.Add("前方一致");
                    this.jsfsKay.Items.Add("中间一致");
                    this.jsfsKay.Items.Add("后方一致");
                    this.jsfsKay.Items.Add("绝对一致");
                }
                else
                {
                    this.jsfsKay.Items.Add("等于");
                    this.jsfsKay.Items.Add("大于");
                    this.jsfsKay.Items.Add("小于");
                    this.jsfsKay.Items.Add("介于");
                }
                this.jsfsKay.SelectedIndex = 0;
            }
        }

        private void fsCheck_CHeckedChanged(object sender, EventArgs e)
        {
            CheckBox checkBox = sender as CheckBox;
            if (checkBox.Checked)
            {
                this.fsFull = true;
            }
            else
            {
                this.fsFull = false;
            }
        }

        public delegate void btnclick(object sender, EventArgs e);

        private void sBtn_Click(object sender, EventArgs e)
        {
            int index = this.jsxKay.SelectedIndex;
            string tableName = this.DbTitleList[index].ToString().Trim();
            string fildName = this.SearchZdList[index].ToString().Trim();
            using (DbOperHandler _open = new Common().Doh())
            {
                this._DataType = _open.dbType.ToString();
                //Console.WriteLine("数据库类型:"+ _open.odbType.ToString());
                _open.Reset();
                _open.ConditionExpress = "所属表='" + tableName + "' and 字段名称='" + fildName + "'";
                string jslx = _open.GetField("检索途径定义", "类型", false).ToString();
                string zdlx = _open.GetField("检索途径定义", "字段类型", false).ToString();
                string type = this.jsfsKay.Text;
                string languageData = SqlText.BuindPosSql(this.sTxt.Text.ToString().ToUpper()).Replace("?", "_");
                languageData = languageData.Replace("*", "%");
                if (tableName != "情报检索库")
                {
                    languageData = SqlText.GetLanguage(languageData);
                }
                int jgs = 100;
                try
                {
                    jgs = int.Parse(this.xzUpDown.Value.ToString()); ;
                }
                catch { }
                string fileds = " g.主键码, ";
                if (this.FiledsList.Count > 0)
                {
                    foreach(string str in this.FiledsList)
                    {
                        fileds += "g." + str + ",";
                    }
                    if (!string.IsNullOrEmpty(fileds))
                        fileds = fileds.Substring(0, fileds.Length - 1);
                    string sql = SqlText.DigitalizeSql(tableName, fildName, languageData, type, Convert.ToInt32(jslx), this._DataType, this.nullCheck.Checked,this.fsCheck.Checked);

                    if (this._DataType == DatabaseType.MYSQL.ToString())
                    {
                        sql = "SELECT " + fileds + " From 情报检索库 g WHERE " + sql;
                    }
                    else if (this._DataType == DatabaseType.ORACLE.ToString())
                    {
                        sql = "SELECT " + fileds + "   From 情报检索库 g WHERE ROWNUM<" + jgs + " AND " + sql;
                    }
                    else if (_open.odbType.ToString().Equals("ORACLE"))
                    {
                        sql = "SELECT  " + fileds + "   From  情报检索库 g WHERE ROWNUM<" + jgs + " AND " + sql;
                        //sql = "SELECT 主键码, DUMP(题名2, 16) AS 题名,DUMP(责任者2, 16) AS 责任者,DUMP(出版者2, 16) AS 出版者, 出版日期, 版次,标准编码,处理日期,索书号,库键码 From 馆藏书目库 WHERE ROWNUM<" + jgs + " AND " + sql;
                    }
                    else
                    {
                        sql = "SELECT TOP " + jgs + fileds + " From 情报检索库 g WHERE " + sql;
                    }
                    if (!this.bxCheck.Checked)
                        sql = sql + " AND g.库键码=" + this._kjm;

                    string stime = string.Empty, etime = string.Empty;
                    if (this.rcCheck.Checked)
                    {
                        stime = this.sTime.Text;
                        etime = this.eTime.Text;
                        sql += "AND (处理日期 BETWEEN '" + stime + "' AND '" + etime + "') ";
                    }
                    if (this.pfsKay.SelectedIndex == 1)
                        sql += " order by " + this.pzdKay.Text + " DESC";
                    else
                        sql += " order by " + this.pzdKay.Text;
                    this.Sql = sql;
                    //Console.WriteLine(sql);
                    if (this.BtnScarchClick != null)
                    {
                        this.BtnScarchClick(sender, e);
                    }
                }
            }
        }

        private void stxt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                this.sBtn_Click(sender, e);
            }
        }
    }
}
